//提供默认的路径
//提供文本本地化二次解析

const { ccclass, property } = cc._decorator;

class UIManger {
	closeType = {
		hide: 0, //隐藏节点，设置节点active=false
		destory: 1, //销毁节点，不释放预制体资源
		//	destoryAndRelease: 2 //销毁节点并且释放预制体资源
	};
	panelNodes = {};
	panelPrefabs = {};
	loadingPrefabs = {};
	closingPrefabs = {};
	// gameHall_res_cache = {};
	// common_res_cache = {};
	// game_res_cache = {}; 

	curPath: string = "";

	complete_callback: Function = null;
	progress_callback: Function = null;

	solidifyRoot: cc.Node = null;

	init(node: cc.Node) {
		this.solidifyRoot = node || this.solidifyRoot;
		this.panelPrefabs = {};
		this.panelNodes = {};
		this.loadingPrefabs = {};
		this.closingPrefabs = {};
	}

	open(path: string, parent: cc.Node = null, callBack = null, ...args) {
		cc.log("on open:", path)
		if (this.loadingPrefabs[path]) {
			return;
		}
		if (this.panelNodes[path]) {
			if (!this.panelNodes[path].active) {
				this._open(null, path, parent, callBack, args);
			} else {
				var _name = path.split("/");
				var name = _name[_name.length - 1];
				if (!this.panelNodes[path]) {
					console.log("11111")
				}
				var _src = this.panelNodes[path].getComponent(name);
				if (_src && _src.awake) {
					_src.awake(args);
				}
				if (callBack) {
					callBack();
				}
			}
			return;
		}
		let _prefab = this.panelPrefabs[path];
		if (_prefab) {
			this._open(_prefab, path, parent, callBack, args);
		} else {
			this.loadingPrefabs[path] = true;

			cc.loader.loadRes(path, cc.Prefab,
				function (err, prefab) {
					console.log("load prefab finish", err, prefab);
					this.loadingPrefabs[path] = false;
					if (this.closingPrefabs[path]) {
						this.close(path, this.closeType.destory);
						return;
					}
					if (err) {
						cc.error(path + " prefab loading failed!");
						if (callBack) {
							callBack(err);
						}
						return;
					}
					this.panelPrefabs[path] = prefab;
					this._open(prefab, path, parent, callBack, args);
				}.bind(this)

			);
		}
	}

	_open(prefab: cc.Prefab, path: string, parent: cc.Node, callBack, args) {
		cc.log("open ready:", path)
		var _node = this.panelNodes[path];
		if (prefab) {
			_node = cc.instantiate(prefab);
			if (!_node) {
				return;
			}
			this.panelNodes[path] = _node;
		}
		if (parent) {
			_node.parent = parent;
		}
		else {
			_node.parent = this.solidifyRoot;
			_node.setSiblingIndex(this.solidifyRoot.childrenCount - 1);
		}
		_node.active = true;
		var _name = path.split("/");
		var name = _name[_name.length - 1];
		var _src = _node.getComponent(name);
		if (callBack) {
			callBack(_src);
		}
		if (_src && _src.init) {
			_src.init(args);
		}
	}

	getOpen(path: string) {
		var _node = this.panelNodes[path];
		return _node;
	}

	//type : 0-设置节点 active = false
	//        1-销毁节点
	//        2-销毁节点并且释放预制体
	close(path: string, type = this.closeType.destory) {
		console.log("close prefab ", path);
		if (this.loadingPrefabs[path]) {
			this.closingPrefabs[path] = true;
			return;
		}
		this.closingPrefabs[path] = false;
		var node: cc.Node = this.panelNodes[path];
		if (node) {
			if (type == this.closeType.hide) {
				node.active = false;
			} else {

				// if (node.isValid)
				node.removeFromParent();
				node.destroy();
				delete this.panelNodes[path];

				// this.panelNodes[path].removeAllChildren(true);
				// cc.vv.resLoader.getInstance().releaseRes(this.panelPrefabs[path]);
				cc.loader.releaseAsset(this.panelPrefabs[path]);
				delete this.panelPrefabs[path];
				// if (type == this.closeType.destoryAndRelease) {
				// 	let _prefab = this.panelPrefabs[path];
				// 	let _deps = cc.loader.getDependsRecursively(_prefab);
				// 	cc.loader.release(_deps);
				// 	delete this.panelPrefabs[path];
				// }
			}
		}
	}
	
	closeAll() {

		// for (var key in this.panelNodes) {
		// 	this.panelNodes[key].destroy();
		// 	delete this.panelNodes[key];
		// }

		for (var key in this.panelPrefabs) {
			this.close(key);
			// cc.loader.releaseAsset(this.panelPrefabs[key]);
			// delete this.panelPrefabs[key];
		}

	}
	//子游戏热更完成后强制释放一次已经打开过的子游戏预制体
	forceRelease(path: string) {
		let _prefab = this.panelPrefabs[path];
		if (_prefab) {
			//这个游戏打开过
			for (var key in this.panelPrefabs) {
				let _prefab = this.panelPrefabs[key];
				let _deps = cc.loader.getDependsRecursively(_prefab);
				cc.loader.release(_deps);
				delete this.panelPrefabs[key];
			}
			for (var key in this.panelNodes) {
				let _prefab = this.panelNodes[key];
				let _deps = cc.loader.getDependsRecursively(_prefab);
				cc.loader.release(_deps);
				delete this.panelNodes[key];
			}
			//清理子游戏预制体依赖资源的时候  会把其他预制体使用的公共资源也清理掉。这里把所有预制体全部清理掉 所有弹窗资源都重新load
			this.panelNodes = {};
			this.panelPrefabs = {};
			this.loadingPrefabs = {};
			this.closingPrefabs = {};
		}
	}

	//预加载预制体，return cc.Node
	preLoadPrefab(path: string, parent: cc.Node, callBack) {
		cc.loader.loadRes(path, (err, prefab) => {
			if (err) {
				cc.vv.GLUtils.showAlert1(path + " prefab loading failed!");
			}
			if(!cc.isValid(parent)){
				return;
			}
			if (prefab) {
				let nodeTmp = cc.instantiate(prefab);
				if (nodeTmp ) {
					nodeTmp.parent = parent;
					nodeTmp.setPosition(cc.v2(0, 0));
					callBack && callBack(nodeTmp);
				}
			} else {
				console.error(err);
			}
		})
	}
}

export let UIMgr: UIManger = new UIManger();
